package org.example.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 健康检查控制器
 * 
 * @author example
 */
@RestController
@RequestMapping("/api")
public class HealthController {
    
    @Value("${spring.application.name}")
    private String applicationName;
    
    @Value("${server.port}")
    private String serverPort;
    
    /**
     * 健康检查接口
     */
    @GetMapping("/health")
    public Map<String, Object> health() {
        Map<String, Object> response = new HashMap<>();
        response.put("status", "UP");
        response.put("application", applicationName);
        response.put("port", serverPort);
        response.put("timestamp", LocalDateTime.now());
        response.put("message", "Spring Cloud Demo 应用运行正常");
        return response;
    }
    
    /**
     * 应用信息接口
     */
    @GetMapping("/info")
    public Map<String, Object> info() {
        Map<String, Object> response = new HashMap<>();
        response.put("applicationName", applicationName);
        response.put("version", "1.0.0");
        response.put("description", "Spring Cloud 学习项目");
        response.put("author", "example");
        response.put("buildTime", LocalDateTime.now());
        
        Map<String, String> features = new HashMap<>();
        features.put("Spring Boot", "2.7.18");
        features.put("Spring Cloud", "2021.0.8");
        features.put("Spring Data JPA", "集成");
        features.put("MySQL", "支持");
        features.put("Eureka Client", "集成");
        response.put("features", features);
        
        return response;
    }
    
    /**
     * 欢迎页面
     */
    @GetMapping("/")
    public Map<String, Object> welcome() {
        Map<String, Object> response = new HashMap<>();
        response.put("message", "欢迎使用 Spring Cloud Demo 应用！");
        response.put("application", applicationName);
        response.put("timestamp", LocalDateTime.now());
        response.put("endpoints", new String[]{
            "GET /api/health - 健康检查",
            "GET /api/info - 应用信息",
            "GET /api/users - 获取所有用户",
            "POST /api/users - 创建用户",
            "GET /api/users/{id} - 根据ID获取用户",
            "PUT /api/users/{id} - 更新用户",
            "DELETE /api/users/{id} - 删除用户",
            "GET /api/users/search?username=xxx - 搜索用户",
            "GET /api/users/stats - 用户统计"
        });
        return response;
    }
}